/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.remove.view;

import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.common.view.DialogApplet;
import offset.nodes.client.dialog.remove.model.HasReferences;
import offset.nodes.client.dialog.remove.model.RemoveNode;

/**
 *
 * @author Walter Lütgenau
 */
public class RemoveNodeApplet extends DialogApplet {

    ServerModel model = null;
    String path = null;
    String repository = null;

    public void startUI() {
        try {
            HasReferences.Response response = (HasReferences.Response) model.sendRequest(new HasReferences.Request(path));
            if (response.getReferences() != HasReferences.Response.ReferenceResult.noReference) {
                showReferencesDialog(response.getReferences());
                return;
            } else {
                if (showRemoveConfirmationDialog() == JOptionPane.OK_OPTION) {
                    RemoveNode.Response removeNodeResponse = (RemoveNode.Response) model.sendRequest(new RemoveNode.Request(path));
                    path = removeNodeResponse.getNewPath();
                }
            }

            URL url = new URL(repository + "/" + path);
            getAppletContext().showDocument(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * There are references.
     * 
     * Show appropriate dialog.
     * 
     * @param result the kind of references
     */
    protected void showReferencesDialog(HasReferences.Response.ReferenceResult result) {
        String message = "";
        switch (result) {
            case hasReference:
                message = getBundle().getString("remove.hasReferences");
                break;

            case descendantHasReference:
                message = getBundle().getString("remove.descendantsHasReferences");
                break;
        }
        JOptionPane pane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE);
        JDialog dialog = pane.createDialog(getBundle().getString("remove.title"));
        setDialogLocation(dialog);
        dialog.setVisible(true);
    }

    /**
     * Show a confirmation dialog to remove the node.
     * 
     * @return the result of the confirmation, JOptionPane.OK_OPTION or JOptionPane.CANCEL_OPTION
     */
    protected int showRemoveConfirmationDialog() {
        JOptionPane pane = new JOptionPane(getBundle().getString("remove.confirm"), JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
        JDialog dialog = pane.createDialog("remove.title");
        setDialogLocation(dialog);
        dialog.setVisible(true);
        
        return ((Integer) pane.getValue()).intValue();
    }

    public void initUI() {
        try {
            path = getParameter("path");
            repository = getParameter("repository");
            String url = getParameter("service");

            model = new ServerModel(new URL(url));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ServerModel getModel() {
        return model;
    }

    public String getPath() {
        return path;
    }

    public String getRepository() {
        return repository;
    }
}
